/**
 * Demo purpose In-SideFX (Un)decorator for JavaFX stage License: You can use
 * this code for any kind of purpose, commercial or not.
 */
package org.misty.undecorator.demoapp;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Slider;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.misty.undecorator.Undecorator;
import org.misty.undecorator.UndecoratorScene;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author in-sideFX
 */
public class UndecoratorStageDemo extends Application implements Initializable {

    Stage primaryStage;
    Undecorator undecorator;
    @FXML
    private AreaChart areaChart;
    @FXML
    private PieChart pieChart;
    @FXML
    private Slider sliderOpacity;

    @Override
    public void start(final Stage stage) throws Exception {
        primaryStage = stage;
        primaryStage.setTitle("Undecorator Stage Demo");
        // The UI (Client Area) to display
        Region root = null;
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ClientArea.fxml"));
            fxmlLoader.setController(this);
            root = fxmlLoader.load();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        undecorator = new Undecorator(stage, root);
        // Customize it by CSS if needed:
//        undecorator.getStylesheets().add("org/misty/undecorator/skin/undecorator.css");

        // Optional: Enable this node to drag the stage
        // By default the root argument of Undecorator is set as draggable
        Node node = root.lookup("#draggableNode");
        undecorator.setAsStageDraggable(stage, node);

        Scene scene = new Scene(undecorator);

        // Install default Accelerators in the Scene
        undecorator.installAccelerators(scene);
        // Enable fade transition
        undecorator.setFadeInTransition();
  /*
         * Fade transition on window closing request
         */
        primaryStage.setOnCloseRequest(we -> {
            we.consume();   // Do not hide
            undecorator.setFadeOutTransition();
        });

        // Transparent scene and stage
        scene.setFill(Color.TRANSPARENT);
        stage.initStyle(StageStyle.TRANSPARENT);

        stage.setScene(scene);
        // Feed Charts with fake data for demo
//        initCharts();

        stage.show();

        // Set minimum size
        stage.setMinWidth(undecorator.getMinWidth());
        stage.setMinHeight(undecorator.getMinHeight());

    }

    @FXML
    public void handleShowUtilityStage(ActionEvent event) {
        // Stage Utility usage
        Region root = null;
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ClientAreaUtility.fxml"));
            fxmlLoader.setController(this);
            root = fxmlLoader.load();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Stage utilityStage = new Stage();
        utilityStage.setTitle("Stage Utility type demo");
        UndecoratorScene scene = new UndecoratorScene(utilityStage, StageStyle.UTILITY, root, null);
        utilityStage.setScene(scene);
        utilityStage.initModality(Modality.WINDOW_MODAL);
        utilityStage.initOwner(primaryStage);

        // Set sizes based on client area's sizes
        Undecorator undecorator = scene.getUndecorator();
        utilityStage.setMinWidth(undecorator.getMinWidth());
        utilityStage.setMinHeight(undecorator.getMinHeight());
        utilityStage.setWidth(undecorator.getPrefWidth());
        utilityStage.setHeight(undecorator.getPrefHeight());
        if (undecorator.getMaxWidth() > 0) {
            utilityStage.setMaxWidth(undecorator.getMaxWidth());
        }
        if (undecorator.getMaxHeight() > 0) {
            utilityStage.setMaxHeight(undecorator.getMaxHeight());
        }
        utilityStage.sizeToScene();
        utilityStage.show();
    }

    @FXML
    public void handleShowNonResizableStage(ActionEvent event) {
        UndecoratorStageDemo undecoratorStageDemo = new UndecoratorStageDemo();
        Stage stage = new Stage();
        stage.setTitle("Not resizable stage");
        stage.setResizable(false);
        stage.setWidth(600);
        stage.setMinHeight(400);
        try {
            undecoratorStageDemo.start(stage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @FXML
    public void handleUtilityAction(ActionEvent event) {
        ((Node) event.getSource()).getScene().getWindow().hide();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        sliderOpacity.valueProperty().addListener((observable, oldValue, newValue) -> {
            undecorator.setBackgroundOpacity(newValue.doubleValue());
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
